﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sausage.Data;

namespace Sausage.Domain
{
    public class MenuInfo
    {
        public MenuCategory Category;
        public Menu Menu;
    }

    public class MenuHelper
    {
        public static List<MenuInfo> GetMenuListByRole(Role targetRole)
        {
            using (var context = new SausageEntities())
            {
                return (from menu in context.Menu
                        join menuCategory in context.CategoryMenu on menu.MenuID equals menuCategory.MenuID
                        join category in context.MenuCategory on menuCategory.CategoryID equals category.CategoryID
                        join menuRole in context.MenuRole on menu.MenuID equals menuRole.MenuID
                        join role in context.Role on menuRole.RoleID equals role.RoleID
                        where role.RoleID == targetRole.RoleID
                        orderby category.TheOrder ascending, menu.TheOrder ascending
                        select new MenuInfo
                                   {
                                       Menu = menu,
                                       Category = category,
                                   }
                       ).ToList();
            }
        }
    }
}
